// pages/login/login.js
const app = getApp()
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    share: util.share,
    checkname:'获取验证码',
    checkcount:60,
    disabled:false,
    phone:'',//手机号
    phonecode:'',//手机验证码
    wxphone:null, //微信授权获取的手机号
    code:'',
    user_id:'', //用户邀请id
  },
  onShareAppMessage: function () {
    return util.share()
  },
  onShow:function(){
    var that= this;
    wx.login({
      success: res => {
        wx.removeStorageSync('code');
        that.setData({
          code:res.code
        });
        
        wx.setStorage({
          key: 'code',
          data: res.code
        });
      }
    });
  },
  onLoad:function(options){
    this.setData({
      user_id:options.user_id
    })
  },
 /**
  * 事件 操作事件
  */
  //获取填写的手机号
      getphone: function (e) {
        this.setData({
          phone: e.detail.value
        })
      },
      getcode: function (e) {
        this.setData({
          phonecode: e.detail.value
        })
      },
     //获取验证码
      getchecknumber:function(e){
        //手机号验证
        if (this.data.phone == ""){
          wx.showToast({
            title: '填写手机号',
            icon: 'none',
            duration: 2000
          });
          return false;
        }

        var that = this;
        const data = {
          wiki:'phone',
          phone: that.data.phone
        }
        http.request({
          url: 'mini/v1/login/send',
          data: data,
          methods:'post',
          success: function (res) {
            wx.showToast({
              title: '已发送，注意查收',
              icon: 'success',
              mask: true,
              duration: 2000
            });
            
            //验证码倒计时开始
                var countDown = setInterval(function () {
                  if (that.data.checkcount <= 0) {
                    that.setData({
                      checkcount: 60,
                      checkname: '获取验证码',
                      disabled: false
                    })
                    clearInterval(countDown)
                    return
                  }
                  that.data.checkcount--
                  that.setData({
                    checkcount: that.data.checkcount,
                    disabled: true,
                    checkname: that.data.checkcount < 10 ? `${that.data.checkcount}s` : `${that.data.checkcount}s`
                  })
                }, 1000);
            //验证码倒计时结束
          }
        })
      },
     //获取手机号登陆
      getPhoneNumber: function (e) {
        if (e.detail.encryptedData){
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          //获取手机号
          var that = this;
          const data = { 
                          code: wx.getStorageSync('code'), 
                          iv: e.detail.iv, 
                          encryptedData: e.detail.encryptedData ,
                          user_id:!!this.data.user_id ? this.data.user_id : ''
                      };
          http.request({
            url: 'mini/v1/login/index?wiki=wechatphone',
            data: data,
            success: function (res) {
              wx.setStorageSync('token', res.data.auth.token_type + '' + res.data.auth.access_token);
              that.setData({
                wxphone: res.data.phone
              });
              wx.navigateTo({
                url: '../chooserole/chooserole?id='+res.data.is_make
              })
            }
          })
        }
      },
    //手机号验证码登录
      submit(){
        if (this.data.phone == "") {
          wx.showToast({
            title: '填写手机号',
            icon: 'none',
            duration: 2000
          });

          return false;
        }

        if (this.data.phonecode == "") {
          wx.showToast({
            title: '填写短信验证码',
            icon: 'none',
            duration: 2000
          });

          return false;
        }
        
        //登录
        var that = this;
        const data = { 
                       wiki: 'username', 
                       username: that.data.phone, 
                       code: that.data.phonecode,
                       user_id:!!this.data.user_id ? this.data.user_id : ''
                      };
        http.request({
          url: 'mini/v1/login/index',
          data: data,
          methods:'post',
          success: function (res) {
            wx.setStorageSync('token', res.data.auth.token_type + '' + res.data.auth.access_token);
              that.setData({
                wxphone: res.data.phone
              });
              wx.navigateTo({
                url: '../chooserole/chooserole?id='+res.data.is_make
              })
          }
        })  
      }
})